<template>
  <div>
    <BasicModal
      @register="register"
      :title="title"
      width="600px"
      :destroyOnClose="true"
      :canFullscreen="false"
      :showOkBtn="false"
      :showCancelBtn="false"
    >
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { ref } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';
  import { IDictCategorySaveData } from '@/api/platform/dictionary/type';
  import { DeviceAdd, DeviceUpdate } from '@/api/device/deviceList';

  const title = ref('新建设备');
  const type = ref('add');
  const { createMessage } = useMessage();

  const emit = defineEmits(['reload']);

  const typeMap = {
    add: '新建设备',
    edit: '编辑设备',
  };

  interface formData {
    userVersion: string;
    userDesc: string;
    templateId: number;
    tenantIds: number[];
  }

  const form = ref<formData>({
    userVersion: '',
    userDesc: '',
    templateId: 0,
    tenantIds: [],
  });

  const [registerForm, { validate, setProps, setFieldsValue, updateSchema }] = useForm({
    schemas: [
      {
        field: 'id',
        component: 'Input',
        label: '字典主键',
        show: false,
      },
      {
        field: 'printerNo',
        component: 'Input',
        required: true,
        label: '设备编号',
        itemProps: {
          extra: '（请查看设备底部条形码下方的编号，并准确输入）',
        },
      },
      {
        field: 'remark',
        component: 'InputTextArea',
        label: '设备说明',
        componentProps: {
          rows: 4,
        },
      },
    ],
    autoFocusFirstItem: true,
    actionColOptions: {
      span: 24,
    },
    labelWidth: 120,
    baseColProps: { span: 20 },
    submitButtonOptions: {
      text: '确认',
    },
    resetButtonOptions: {
      text: '取消',
    },
    submitFunc: async () => {
      const data: IDictCategorySaveData = await validate();
      setProps({
        submitButtonOptions: {
          loading: true,
        },
      });
      console.log(data, form.value);
      try {
        (data.id && (await DeviceUpdate({ ...data }))) ||
          (await DeviceAdd({ ...data, tenantId: 0 }));
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });

        createMessage.success(`${typeMap[type.value]}成功`);
        emit('reload');
        closeModal();
      } catch (error) {
        console.log('error--->', error);
        setProps({
          submitButtonOptions: {
            loading: false,
          },
        });
      }
    },
    resetFunc: async () => {
      closeModal();
    },
  });

  const [register, { closeModal }] = useModalInner(async (data) => {
    // console.log('data--->', data);
    type.value = data.type;

    data.row && setFieldsValue(data.row);
    updateSchema({
      field: 'printerNo',
      componentProps: {
        disabled: type.value === 'edit',
      },
    });

    title.value = typeMap[data.type];
  });
</script>

<style lang="less" scoped></style>
